package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.asset.model.entity.AssetHistory;
import com.erpanderp.wam.asset.model.entity.AssetMaster;
import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetHistoryDAO;
import com.erpanderp.wam.asset.model.service.AssetMasterDAO;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class AssetHandoverDetailsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	private ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();
	private Map<String,String> assetStatusMap = CommonLookup.getAssetStatus();
	
	 private AssetRequest assetRequest;
	 private AssetHistory assetHistory=new AssetHistory();
	 
	 private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	 private AssetHistoryDAO assetHistoryDAO=new AssetHistoryDAO();
	 private AssetMasterDAO assetMasterDAO=new AssetMasterDAO();
	 
	 private List<AssetHistory> assetDetailsList = new ArrayList<AssetHistory>(); 
	 
	 private String assetCode;
	 private boolean FORM_VALID;
	 private String assetStatus;
	 
	 public AssetHandoverDetailsBean() {
		assetCode=request.getParameter("assetCode");
		System.out.println("============assetCode BEAM CONST============"+assetCode);
		assetRequest = assetRequestDAO.getAssetRequestByAssetCode(assetCode);
		System.out.println("============getAssetRequestId IN BEAM CONST============"+assetRequest.getAssetRequestId());
		if(assetRequest != null){
			assetDetailsList=assetHistoryDAO.getAssetHistoryDetailsByAssetRequest(assetRequest.getAssetRequestId());
			for(AssetHistory as:assetDetailsList){
				System.out.println("=============================");
				System.out.println("ASSET TYPE ==="+as.getAssetDetails().getAssetType());
				System.out.println("ASSET CODE ===="+as.getAssetDetails().getAssetReferenceCode());
			}
			
		}
		
	}
	
	public String assetHandoverListener(ActionEvent ae){
		
		validateForm();
		if(FORM_VALID){	 
		 try{
			 assetHistory.setAssetStatus("RETURNED");
			 assetHistory.setAssetHandOverTo(wamUser.getEmployee());
			 assetHistory.setHandOverDate(new Date());
			 assetHistory.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			 assetHistory.setDateModified(new Date());
			 assetHistoryDAO.saveOrUpdateAssetHistory(assetHistory);			 
			 
			 AssetMaster updateAssetMaster =assetHistory.getAssetMaster();
			 updateAssetMaster.setHandOverDate(new Date());
			 updateAssetMaster.setHandOverTo(wamUser.getEmployee());
			 updateAssetMaster.setHandOverRemarks(assetHistory.getHandoverRemarks());
			 updateAssetMaster.setAssetStatus(assetStatus);
			 updateAssetMaster.setDateModified(new Date());
			 updateAssetMaster.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			 assetMasterDAO.saveOrUpdateAssetMaster(updateAssetMaster);
			 
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/assethandoverdetails.html?assetCode="+assetCode);
			
			  }catch (Exception e) {
				  e.printStackTrace();
			  }
			 return "assethandoverdetails";	
		}else{
			RequestContext.getCurrentInstance().execute("assetHandoverDialog.show();");
			return null;
		}
    }
		
	private void validateForm(){
		FORM_VALID=true;
		System.out.println("=============sssssssss===================="+assetHistory.getHandoverRemarks());
		if(ValidationUtil.isEmpty(assetHistory.getHandoverRemarks(), "Final Remarks", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}			
	}

	//GETTERS AND SETTERS
	public TimeZone getTimeZone() {
		return timeZone;
	}
	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public AssetRequest getAssetRequest() {
		return assetRequest;
	}
	public void setAssetRequest(AssetRequest assetRequest) {
		this.assetRequest = assetRequest;
	}
	
	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}
	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}
	
	public String getAssetCode() {
		return assetCode;
	}
	public void setAssetCode(String assetCode) {
		this.assetCode = assetCode;
	}

	public AssetHistoryDAO getAssetHistoryDAO() {
		return assetHistoryDAO;
	}
	public void setAssetHistoryDAO(AssetHistoryDAO assetHistoryDAO) {
		this.assetHistoryDAO = assetHistoryDAO;
	}

	public List<AssetHistory> getAssetDetailsList() {
		return assetDetailsList;
	}
	public void setAssetDetailsList(List<AssetHistory> assetDetailsList) {
		this.assetDetailsList = assetDetailsList;
	}

	public AssetHistory getAssetHistory() {
		return assetHistory;
	}
	public void setAssetHistory(AssetHistory assetHistory) {
		this.assetHistory = assetHistory;
	}

	public Map<String,String> getAssetStatusMap() {
		return assetStatusMap;
	}

	public void setAssetStatusMap(Map<String,String> assetStatusMap) {
		this.assetStatusMap = assetStatusMap;
	}

	public String getAssetStatus() {
		return assetStatus;
	}

	public void setAssetStatus(String assetStatus) {
		this.assetStatus = assetStatus;
	}

	public AssetMasterDAO getAssetMasterDAO() {
		return assetMasterDAO;
	}

	public void setAssetMasterDAO(AssetMasterDAO assetMasterDAO) {
		this.assetMasterDAO = assetMasterDAO;
	}

}
